package com.huawei.appgallery.foundation.ui.framework.widget;

import android.content.Context;
import android.os.BadParcelableException;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import java.util.HashMap;

import o.SomeFragmentStateAdapter;
import o.Logger;
import o.ContentManager;
import o.MinimumFactory;
import o.ClassParcel;
import o.ClassParcelImpl;
import o.PagerAdapter;

public class FixViewPager extends ViewPager {
    private boolean isPagingEnabled = true;
    private int mIndex = 0;
    private HashMap<OnPageChangeListener, FixVPListener> mCachedListener = new HashMap();

    public static class SavedState implements Parcelable {
        public static final Creator<SavedState> CREATOR = ClassParcelImpl.creat(new ClassParcel<SavedState>() {
            public /* synthetic */ SavedState[] parcelBackArr(int i) {
                return getParArr(i);
            }

            public /* synthetic */ SavedState parcelClsPar(Parcel parcel, ClassLoader classLoader) {
                return getClsLoader(parcel, classLoader);
            }

            public SavedState getClsLoader(Parcel parcel, ClassLoader classLoader) {
                return new SavedState(parcel, classLoader);
            }

            public SavedState[] getParArr(int i) {
                return new SavedState[i];
            }
        });
        private Parcelable mParce;
        private final int mIndex;

        private SavedState(Parcelable parcelable, int i) {
            this.mParce = parcelable;
            this.mIndex = i;
        }

        private SavedState(Parcel parcel, ClassLoader classLoader) {
            if (classLoader == null) {
                classLoader = getClass().getClassLoader();
            }
            try {
                this.mParce = parcel.readParcelable(classLoader);
            } catch (BadParcelableException e) {
                Logger.e("FixViewPager", "BadParcelableException:" + e.getMessage() + ",loader=" + classLoader);
                this.mParce = parcel.readParcelable(SavedState.class.getClassLoader());
            }
            this.mIndex = parcel.readInt();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeParcelable(this.mParce, i);
            parcel.writeInt(this.mIndex);
        }
    }

    class FixVPListener implements OnPageChangeListener {
        final /* synthetic */ FixViewPager adapter;
        private final OnPageChangeListener listener;

        public FixVPListener(FixViewPager fixViewPager, OnPageChangeListener aVar) {
            this.adapter = fixViewPager;
            this.listener = aVar;
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int width = this.adapter.getWidth();
            PagerAdapter adapter = FixViewPager.this.getAdapter();
            if (this.adapter.isLTR(adapter)) {
                int count = adapter.getCount();
                int pageOffset = ((int) (((float) width) * (1.0f - adapter.getPageWidth(position)))) + positionOffsetPixels;
                while (position < count && pageOffset > 0) {
                    position++;
                    pageOffset -= (int) (((float) width) * adapter.getPageWidth(position));
                }
                position = (count - position) - 1;
                positionOffsetPixels = -pageOffset;
                positionOffset = ((float) positionOffsetPixels) / (((float) width) * adapter.getPageWidth(position));
            }
            this.listener.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        public void onPageScrollStateChanged(int position) {
            PagerAdapter adapter = FixViewPager.this.getAdapter();
            if (this.adapter.isLTR(adapter)) {
                position = (adapter.getCount() - position) - 1;
            }
            this.listener.onPageScrollStateChanged(position);
        }

        public void onPageScrolled(int i) {
            this.listener.onPageScrolled(i);
        }
    }

    public FixViewPager(Context context) {
        super(context);
    }

    public FixViewPager(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public void onRtlPropertiesChanged(int i) {
        int circelStartIndex = 1;
        int normalStartIndex = 0;
        super.onRtlPropertiesChanged(i);
        if (i != 1) {
            circelStartIndex = 0;
        }
        if (circelStartIndex != this.mIndex) {
            PagerAdapter adapter = super.getAdapter();
            if (adapter != null) {
                normalStartIndex = getCurrentItem();
            }
            this.mIndex = circelStartIndex;
            if (adapter != null) {
                adapter.notifyDataSetChanged();
                setCurrentItem(normalStartIndex);
            }
        }
    }

    public void setIsCanScroll(boolean scrollable) {
        this.isPagingEnabled = scrollable;
    }

    public PagerAdapter getAdapter() {
        if (super.getAdapter() instanceof SomeFragmentStateAdapter) {
            return (SomeFragmentStateAdapter) super.getAdapter();
        }
        return super.getAdapter();
    }

    public void setAdapter(PagerAdapter ozVar) {
        super.setAdapter(ozVar);
        setCurrentItem(0);
    }

    private boolean isLTR(PagerAdapter adapter) {
        return adapter != null && (adapter instanceof SomeFragmentStateAdapter) && MinimumFactory.getBoolValue(ContentManager.getInstant().getContext());
    }

    public int getCurrentItem() {
        int item = super.getCurrentItem();
        PagerAdapter adapter = super.getAdapter();
        if (isLTR(adapter)) {
            return (adapter.getCount() - item) - 1;
        }
        return item;
    }

    public void setCurrentItem(int i) {
        PagerAdapter adapter = super.getAdapter();
        if (isLTR(adapter)) {
            i = (adapter.getCount() - i) - 1;
        }
        super.setCurrentItem(i);
    }

    public void setCurrentItem(int item, boolean smoothScroll) {
        PagerAdapter adapter = super.getAdapter();
        if (isLTR(adapter)) {
            item = (adapter.getCount() - item) - 1;
        }
        super.setCurrentItem(item, smoothScroll);
    }

    public Parcelable onSaveInstanceState() {
        return new SavedState(super.onSaveInstanceState(), this.mIndex);
    }

    public void onRestoreInstanceState(Parcelable parcelable) {
        if (parcelable instanceof SavedState) {
            SavedState savedState = (SavedState) parcelable;
            this.mIndex = savedState.mIndex;
            super.onRestoreInstanceState(savedState.mParce);
            return;
        }
        super.onRestoreInstanceState(parcelable);
    }

    public void setOnPageChangeListener(OnPageChangeListener aVar) {
        super.setOnPageChangeListener(new FixVPListener(this, aVar));
    }

    public void addOnPageChangeListener(OnPageChangeListener aVar) {
        FixVPListener dVar = new FixVPListener(this, aVar);
        this.mCachedListener.put(aVar, dVar);
        super.addOnPageChangeListener(dVar);
    }

    public void removeOnPageChangeListener(OnPageChangeListener aVar) {
        super.removeOnPageChangeListener((OnPageChangeListener) this.mCachedListener.get(aVar));
    }

    public void onMeasure(int i, int i2) {
        if (MeasureSpec.getMode(i2) == MeasureSpec.UNSPECIFIED) {
            int i3 = 0;
            for (int i4 = 0; i4 < getChildCount(); i4++) {
                View childAt = getChildAt(i4);
                childAt.measure(i, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED));
                int measuredHeight = childAt.getMeasuredHeight();
                if (measuredHeight > i3) {
                    i3 = measuredHeight;
                }
            }
            i2 = MeasureSpec.makeMeasureSpec(i3, MeasureSpec.AT_MOST);
        }
        super.onMeasure(i, i2);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.isPagingEnabled) {
            return super.onTouchEvent(motionEvent);
        }
        return false;
    }

    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        boolean z = false;
        if (this.isPagingEnabled) {
            try {
                z = super.onInterceptTouchEvent(motionEvent);
            } catch (Exception e) {
                Logger.e("FixViewPager", "onInterceptTouchEvent, ex: " + e.toString());
            }
        }
        return z;
    }
}
